//
//  Gift_GoodsView.m
//  MindBox
//
//  Created by MS on 16-1-9.
//  Copyright (c) 2016年 Lemon. All rights reserved.
//

#import "Gift_GoodsView.h"
#import "UIImageView+WebCache.h"
#import "Week_ItemView.h"
#import "Gift_Goods_Cell.h"
#import "UIView+Frame.h"
@interface Gift_GoodsView ()

@property (nonatomic,strong) NSMutableArray * itemArray;
@property (weak, nonatomic) IBOutlet UILabel *titleView;


@end

@implementation Gift_GoodsView

-(void)setGiftPageData:(GiftPageData *)giftPageData
{
    _giftPageData = giftPageData;
    
    NSString  *title = [NSString stringWithFormat:@"   %@",giftPageData.good_title];
    _titleView.text = title;
    NSArray * goodsArray = giftPageData.goods;
    
    int i=0;
    for (Gift_Goods_Cell * itemView in self.itemArray) {
        
        Gift_Goods * goods = goodsArray[i];
        
        [itemView.imageView sd_setImageWithURL:[NSURL URLWithString:goods.image_url]];
        itemView.titleView.text = goods.short_name;
      
        NSString * price = [NSString stringWithFormat:@"¥ %.2f",[goods.p_price floatValue]];
        itemView.priceView.text = price;
        i++;
    }
    
    [self layoutSubviews];
}

-(NSMutableArray *)itemArray
{
    if (_itemArray==nil) {
        _itemArray = [[NSMutableArray alloc] init];
    }
    return _itemArray;
}

-(void)awakeFromNib
{
//    self.backgroundColor = [UIColor redColor];
    [self createContentViewWithCount:5];
    
}

-(void)createContentViewWithCount:(NSInteger)count
{
    for (int i =0; i<count; i++) {
        Gift_Goods_Cell * itemView = [Gift_Goods_Cell goodsViewCllViewWithXib];
        [self addSubview:itemView];
        [self.itemArray addObject:itemView];
        
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewDidCick:)];
        [itemView addGestureRecognizer:tap];
        itemView.tag = i;
       
    }
    
}

-(void)viewDidCick:(UIGestureRecognizer *)tap
{
    if (self.GoodsViewIndexBlock) {
        _GoodsViewIndexBlock(tap.view.tag);
    }
}

+(instancetype) gift_GoodsViewWithXib
{
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil] lastObject];
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
//    [self.lineView setNeedsLayout];
//    [self.lineView layoutIfNeeded];
    
    CGFloat margin = 1;
    CGFloat y0 = CGRectGetMaxY(self.titleView.frame)+margin;
  
    CGFloat h =( self.height - y0-margin)*0.5;
    CGFloat w0 = (self.width -margin)*0.5;
    CGFloat w1 = (self.width -margin*2)/3;
   // CGFloat margin1 = (self.width-w1*2-margin)/2;
    CGFloat y1 = y0+h+margin;
    int i = 0;
    for (Gift_Goods_Cell * view in self.itemArray) {
        if (i<2) {
            CGFloat x = (margin+w0)*i;
            view.frame = CGRectMake(x, y0, w0, h);
        }
        else
        {
            CGFloat x = (margin+w1)*((i-2)%3);
            view.frame = CGRectMake(x, y1, w1, h);
        }
        i++;
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
